import FormWrap from '@/components/form-wrap/index.vue';

export const useForm = () => {
  // From实例
  const formRef = ref<typeof FormWrap>();
  const formCreateRef = ref();

  const register = (ref: typeof FormWrap, createRef: Ref<any>) => {
    formRef.value = ref;
    formCreateRef.value = createRef;
  };

  const getForm = async () => {
    await nextTick();
    const form = unref(formRef);
    if (!form) {
      console.error(
        'The form is not registered. Please use the register method to register'
      );
    }
    return form;
  };

  // 一些内置的方法
  const methods = {
    /**
     * @description 设置form的值
     * @param data 需要设置的数据
     */
    setValues: async (data: Recordable) => {
      const form = await getForm();
      form?.setValues(data);
    },

    /**
     * @description 设置schema
     * @param field
     * @param schemaProps 需要设置的schemaProps
     */
    setSchema: async (field: string, schemaProps: FormSchema) => {
      const form = await getForm();
      form?.setSchema(field, schemaProps);
    },

    /**
     * @description 新增schema
     * @param formSchema 需要新增数据
     * @param index 在哪里新增
     */
    addSchema: async (formSchema: FormSchema, index?: number) => {
      const form = await getForm();
      form?.addSchema(formSchema, index);
    },

    /**
     * @description 删除schema
     * @param field 删除哪个数据
     */
    delSchema: async (field: string) => {
      const form = await getForm();
      form?.delSchema(field);
    },

    /**
     * @description 获取表单数据
     * @returns form data
     */
    getFormData: async <T = Recordable>(): Promise<T> => {
      const form = await getForm();
      const res = form?.getFormData();
      return res as T;
    },

    submitForm: async <T = Recordable>(): Promise<T> => {
      const form = await getForm();
      const res = await form?.submit();
      return res as T;
    },

    getElFormExpose: async () => {
      await getForm();
    },

    getFormExpose: async () => {
      await getForm();
      return unref(formRef);
    },

    setOptions: async (field: string, options: any) => {
      const form = await getForm();
      form?.setOptions(field, options);
    },

    setProps: async (field: string, props: any) => {
      const form = await getForm();
      form?.setProps(field, props);
    },

    reloadForm: async () => {
      const form = await getForm();
      form?.reload();
    },

    getValue: (field: string) => {
      return formRef.value?.getValue(field);
    },

    setValue: async (field: string, value: any) => {
      const form = await getForm();
      form?.setValues({ [field]: value });
    },

    /**
     * 合并form规则
     * @param field
     * @param rule
     */
    setRule: async (field: string, rule: any) => {
      const form = await getForm();
      form?.setRule(field, rule);
    },

    /**
     * 替换form规则
     * @param field
     * @param rule
     */
    changeRule: async (field: string, rule: any) => {
      const form = await getForm();
      form?.changeRule(field, rule);
    },
  };

  return {
    formRegister: register,
    formMethods: methods,
  };
};
